package com.stylefeng.guns.modular.cloudPlatform.controller;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.stylefeng.guns.config.properties.GunsProperties;
import com.stylefeng.guns.core.common.exception.BizExceptionEnum;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.modular.cloudPlatform.util.CPStr;

@Controller
@RequestMapping(value = "/webuploader")
public class webuploader {

    @Resource
    private GunsProperties gunsProperties;
  
    @ResponseBody
    @PostMapping(value = "/upload")
    public String uploadVA(@RequestPart("file") MultipartFile va){
        //后缀名
        Integer postIndex = va.getOriginalFilename().lastIndexOf(".");
        String post = va.getOriginalFilename().substring(postIndex);
        String vaName = UUID.randomUUID().toString() + post;
//        String path = System.getProperty("java.io.tmpdir");
      //获取upload文件夹得真实路径  
//        String path = req.getSession().getServletContext().getRealPath(CPStr.UPLOAD_ARTICLE_IMG); 
        String path = gunsProperties.getFileUploadPath();
//        if (post.equals(".mp3")){
//            path += "mp3"+File.separator;
//        }
//        else if(post.equals(".mp4") || post.equals(".avi")) {
//            path +=  "avi"+File.separator;
//        }
//        else if (post.equals(".gif") || post.equals(".jpg") || post.equals("jpeg") || post.equals("bmp") || post.equals("png")){
//            path +=  "img"+File.separator;
//        }
        try {
//        	File file = new File(path);
//        	if(!file.exists())
//    			file.mkdirs();	
            va.transferTo(new File(path+vaName));
        } catch (Exception e) {
            throw new GunsException(BizExceptionEnum.UPLOAD_ERROR);
        }
        return vaName;
    }
    /*
     *采用spring提供的上传文件的方法
     */
    @RequestMapping("springUpload")
    public String  springUpload(HttpServletRequest request) throws IllegalStateException, IOException
    {
    	
         long  startTime=System.currentTimeMillis();
         //将当前上下文初始化给  CommonsMutipartResolver （多部分解析器）
        CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver(
                request.getSession().getServletContext());
        //检查form中是否有enctype="multipart/form-data"
        if(multipartResolver.isMultipart(request))
        {
        	
            //将request变成多部分request
            MultipartHttpServletRequest multiRequest=(MultipartHttpServletRequest)request;
           //获取multiRequest 中所有的文件名
            Iterator iter=multiRequest.getFileNames();
             
            while(iter.hasNext())
            {
                //一次遍历所有文件
                MultipartFile file=multiRequest.getFile(iter.next().toString());
                //后缀名
                Integer postIndex = file.getOriginalFilename().lastIndexOf(".");
                String post = file.getOriginalFilename().substring(postIndex);
                 String vaName = UUID.randomUUID().toString() + post;
                if(file!=null)
                {
                    String path="E:/springUpload"+file.getOriginalFilename();
                    //上传
                    file.transferTo(new File(path));
                }
                 return vaName;
            }
           
        }
        long  endTime=System.currentTimeMillis();
        System.out.println("方法三的运行时间："+String.valueOf(endTime-startTime)+"ms");
    return "/success"; 
    }
}
